package com.cheyunkeji.er.adapter.auction;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cheyun.netsalev3.R;
import com.cheyunkeji.er.adapter.MyBaseAdapter;
import com.cheyunkeji.er.app.PushMsgManager;
import com.cheyunkeji.er.bean.auction.NotifyItemBean;
import com.cheyunkeji.er.utils.ImageLoadUtil;
import com.cheyunkeji.er.utils.TimeUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/** Created by Michael on 2017/2/16 15:45. */
public class NotificationItemAdapter extends MyBaseAdapter {

  public NotificationItemAdapter(ArrayList<NotifyItemBean> dataList, Context context) {
    super(dataList, context);
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {

    final ViewHolder holder;
    if (convertView == null) {
      convertView = LayoutInflater.from(context).inflate(R.layout.er_lv_item_liubiao_notify, null);
      holder = new ViewHolder(convertView);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }

    NotifyItemBean itemBean = (NotifyItemBean) dataList.get(position);

    switch (itemBean.getPayload().getNotice_type()) {
      case PushMsgManager.MSG_TYPE_ZHONGBIAO:
        holder.tvDesc.setText(itemBean.getPayload().getBody());
        if (!TextUtils.isEmpty(itemBean.getPayload().getCallprice())) {
          holder.tvPrice.setVisibility(View.VISIBLE);
          holder.tvPrice.setText(
              String.format("得标价格 ： %1$s元", itemBean.getPayload().getCallprice()));
        }
        holder.tvTitle.setText(itemBean.getPayload().getTitle());
        holder.tvTime.setText(
            TimeUtils.millis2StringWithoutSeconds(itemBean.getPayload().getDateline() * 1000));

        if (!TextUtils.isEmpty(itemBean.getPayload().getCover()))
          ImageLoadUtil.loadImg(context, itemBean.getPayload().getCover(), holder.ivCover);
        else ImageLoadUtil.loadLocalImg(context, R.drawable.er_default_img_select_bkg, holder.ivCover);
        break;

      case PushMsgManager.MSG_TYPE_XUNJIA:
      case PushMsgManager.MSG_TYPE_PAIMAI:
      case PushMsgManager.MSG_TYPE_LIUBIAO:
      case PushMsgManager.MSG_TYPE_YUZHAN:
        holder.tvTitle.setText(itemBean.getPayload().getTitle());
        holder.tvTime.setText(
            TimeUtils.millis2StringWithoutSeconds(itemBean.getPayload().getDateline() * 1000));
        holder.tvDesc.setText(itemBean.getPayload().getBody());

        if (!TextUtils.isEmpty(itemBean.getPayload().getCover()))
          ImageLoadUtil.loadImg(context, itemBean.getPayload().getCover(), holder.ivCover);
        else
          ImageLoadUtil.loadLocalImg(context, R.drawable.er_default_img_select_bkg, holder.ivCover);

        break;

      default:
        break;
    }

    return convertView;
  }

  static class ViewHolder {

    @BindView(R.id.iv_cover)
    ImageView ivCover;
    //        @BindView(R.id.iv_tag)
    //        ImageView ivTag;
    @BindView(R.id.tv_title)
    TextView tvTitle;

    @BindView(R.id.tv_desc)
    TextView tvDesc;

    @BindView(R.id.tv_price)
    TextView tvPrice;

    @BindView(R.id.tv_time)
    TextView tvTime;

    ViewHolder(View view) {
      ButterKnife.bind(this, view);
    }
  }
}
